<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>完美电商</title>
    <!-- Bootstrap -->
    <link href="${pageContext.request.contextPath }/static/vendors/bootstrap/dist/css/bootstrap.min.css"
          rel="stylesheet">
    <!-- Font Awesome -->
    <link href="${pageContext.request.contextPath }/static/vendors/font-awesome/css/font-awesome.min.css"
          rel="stylesheet">
    <!-- NProgress -->
    <link href="${pageContext.request.contextPath }/static/vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="${pageContext.request.contextPath }/static/vendors/iCheck/skins/flat/green.css" rel="stylesheet">
    <!-- bootstrap-progressbar -->
    <link href="${pageContext.request.contextPath }/static/vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css"
          rel="stylesheet">
    <!-- JQVMap -->
    <link href="${pageContext.request.contextPath }/static/vendors/jqvmap/dist/jqvmap.min.css" rel="stylesheet"/>

    <!-- Custom Theme Style -->
    <link href="${pageContext.request.contextPath }/static/build/css/custom.min.css" rel="stylesheet">

    <!-- PNotify -->
    <link href="${pageContext.request.contextPath }/static//vendors/pnotify/dist/pnotify.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/static//vendors/pnotify/dist/pnotify.buttons.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/static//vendors/pnotify/dist/pnotify.nonblock.css" rel="stylesheet">
</head>

<body class="nav-md">
<div class="container body">
    <div class="main_container">
        <%@include file="/WEB-INF/jsps/menu.jsp" %>
        <!-- top navigation -->
        <%@include file="/WEB-INF/jsps/top.jsp" %>
        <!-- /top navigation -->

        <!-- page content -->
        <div class="right_col" role="main">
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <%--添加主体内容--%>
                    <div class="x_panel">
                        <div class="x_title">
                            <h2>修改商品分类
                                <small>修改分类</small>
                            </h2>
                            <div class="clearfix"></div>
                        </div>
                        <div class="x_content">

                            <form id="edit_from" class="form-horizontal form-label-left" novalidate>
                                <div class="item form-group">
                                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="cname">选择父类 <span
                                            class="required">*</span>
                                    </label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <select id="heard" name="heard" class="form-control" required>
                                            <c:if test="${category.parentid==0}">
                                                <option value="0">无</option>
                                            </c:if>
                                            <c:if test="${category.parentid!=0}">
                                                <option value="${parentCategory.cid}">${parentCategory.cname}</option>
                                            </c:if>
                                            <option value="0">无</option>
                                            <c:forEach items="${list}" var="li">
                                                <option value="${li.cid}">${li.cname}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>
                                <div class="item form-group">
                                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="cname">分类名<span
                                            class="required">*</span>
                                    </label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <input type="hidden" name="cid" value="${category.cid}"/>
                                        <input id="cname" class="form-control col-md-7 col-xs-12"
                                               data-validate-length-range="6" data-validate-words="2" name="cname"
                                               type="text" value="${category.cname}">
                                        <p id="usernameError" style="margin-bottom: -2px;color: red;">
                                        </p>
                                        <p id="usernameSuccess" style="margin-bottom: -2px;color: green;">
                                        </p>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-md-6 col-md-offset-3">
                                        <a href="${pageContext.request.contextPath}/product/listCategory?pageNum=${pageNum}"
                                           class="btn btn-primary">返回</a>
                                        <button id="addBtn" type="button" class="btn btn-success">修改</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <br/>
        </div>
        <!-- /page content -->

        <!-- footer content -->
        <%@include file="/WEB-INF/jsps/footer.jsp" %>
        <!-- /footer content -->
    </div>
</div>

<!-- jQuery -->
<script src="${pageContext.request.contextPath }/static/vendors/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="${pageContext.request.contextPath }/static/vendors/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="${pageContext.request.contextPath }/static/vendors/fastclick/lib/fastclick.js"></script>
<!-- NProgress -->
<script src="${pageContext.request.contextPath }/static/vendors/nprogress/nprogress.js"></script>
<!-- Chart.js -->
<!-- bootstrap-progressbar -->
<script src="${pageContext.request.contextPath }/static/vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
<!-- iCheck -->
<script src="${pageContext.request.contextPath }/static/vendors/iCheck/icheck.min.js"></script>
<!-- Skycons -->
<script src="${pageContext.request.contextPath }/static/vendors/skycons/skycons.js"></script>
<!-- Custom Theme Scripts -->
<script src="${pageContext.request.contextPath }/static/build/js/custom.min.js"></script>
<!-- PNotify -->
<script src="${pageContext.request.contextPath }/static/vendors/pnotify/dist/pnotify.js"></script>
<script src="${pageContext.request.contextPath }/static/vendors/pnotify/dist/pnotify.buttons.js"></script>
<script src="${pageContext.request.contextPath }/static/vendors/pnotify/dist/pnotify.nonblock.js"></script>

<script>
    $(function () {
        $("#cname").change(function () {
            var cname = $("#cname").val();
            cname = $.trim(cname);
            if (cname != "" && cname != null) {
                data = {cname: cname};
                $.ajax({
                    data: data,
                    url: "${pageContext.request.contextPath}/category/checkCname",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 0) {
                            $("#usernameError").empty();
                            $("#usernameSuccess").empty().html("该分类名可以使用");
                            $("#addBtn").attr("disabled", false);
                        }
                        if (data.code == 1) {
                            $("#usernameSuccess").empty();
                            $("#usernameError").empty().html("该分类名已经存在");
                            $("#addBtn").attr("disabled", true);
                        }
                    }
                });

            } else {
                $("#usernameError").empty().html("分类名不能为空！");
                $("#addBtn").attr("disabled", true);
            }
        });
    });

    $(function () {
        $("#addBtn").click(function () {
            $.ajax({
                data: $("#edit_from").serialize(),
                dataType: "json",
                contentType: "application/json",
                url: "${pageContext.request.contextPath}/category/editCategory",
                success:function (data) {
                    if (data.code == 1) {
                        new PNotify({
                            title: '小贴士',
                            text: '修改分类成功！',
                            type: 'success',
                            styling: 'bootstrap3',
                            hide: true
                        });
                        setTimeout(function () {
                            window.location.href = "${pageContext.request.contextPath}/product/listCategory?pageNum=${pageNum}";
                        }, 1000);
                    } else {
                        new PNotify({
                            title: '小贴士',
                            text: data.msg,
                            delay: 1000,
                            styling: 'bootstrap3',
                            hide: true
                        });
                    }
                }
            });
        });
    });

</script>


</body>
</html>
